package com.pro.addresslist.dal.bll;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.pro.addresslist.dal.model.ContactInfo;
import com.pro.addresslist.dal.model.Friendship;
import com.pro.addresslist.dal.model.Relationship;
import com.pro.addresslist.dal.model.V_FriendBean;
import com.pro.addresslist.tools.EncryptInterface;
import com.pro.addresslist.tools.EncryptRequest;
import com.pro.addresslist.tools.JSONHelper;
import com.pro.addresslist.tools.URLUtil;
import com.pro.addresslist.tools.constantUtil;
/**
 * 
 * @author leekh
 *
 */
public class FriendshipBLL {
	/**
	 * 添加联系人朋友
	 * @param mContext
	 * @param friend
	 */
	public static void AddFriendShip(Context mContext,Friendship friend){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AddFriendShip");
		mMap.put("friend", JSONHelper.toJSON(friend));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "AddFriendShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 if(stResult.length() != 0){;
		        	Log.i("bt","AddFriendShip+value==>"+stResult);
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("AddFriendShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","AddFriendShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 添加联系人朋友（批量）
	 * @param mContext
	 * @param friends
	 */
	public static void AddBatchFriendShip(Context mContext,List<Friendship> friends){
		
	
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AddBatchFriendShip");
		mMap.put("friends", JSONHelper.toJSON(friends));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "AddBatchFriendShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
		        	Log.i("bt","AddBatchFriendShip+value==>"+result);
				 if(stResult.length() != 0){
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("AddBatchFriendShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","AddBatchFriendShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 删除联系人朋友
	 * @param mContext
	 * @param friendID
	 */
	public static void DeleteFriendShip(Context mContext,String friendID){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "DeleteFriendShip");
		mMap.put("friendID", friendID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "DeleteFriendShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 if(stResult.length() != 0){
			        	Log.i("bt","DeleteFriendShip+value==>"+result);

					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("DeleteFriendShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","DeleteFriendShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 编辑联系人朋友
	 * @param mContext
	 * @param friend
	 */
	public static void AlterFriendShip(Context mContext,Friendship friend){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AlterFriendShip");
		mMap.put("friend", JSONHelper.toJSON(friend));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "AlterFriendShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 
				 if(stResult.length() != 0){
			        	Log.i("bt","AlterFriendShip+value==>"+result);

					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("AlterFriendShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","AlterFriendShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人朋友
	 * @param mContext
	 * @param contactID
	 */
	public static void SelectFriendShip(Context mContext,String contactID){
		final List<Friendship> list=new ArrayList<Friendship>();

		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectFriendShip");
		mMap.put("contactID", contactID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "SelectFriendShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {		
					if(result.has("result"))
					{
					 JSONArray jsonArray = result.optJSONArray("result");
					 if(jsonArray!=null){
						 Friendship[] uu=null;
						 uu=JSONHelper.parseArray(jsonArray, Friendship.class);
						 for (Friendship friendship : uu) {
							 list.add(friendship);
					        	Log.i("bt","SelectFriendShip+val==>"+friendship.get_FriendshipID()+">"+friendship.get_ContactMainID()+">"+friendship.get_ContactChildrenID());
						}
						 Intent intent = new Intent(); 
			             Bundle bundle = new Bundle();
			             bundle.putSerializable("parameter",(Serializable)list);
			             intent.putExtras(bundle);
			             intent.setAction("SelectFriendShip");  
			             mContext.sendBroadcast(intent);
					 }
					} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectFriendShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人朋友视图
	 * @param mContext
	 * @param contactID
	 */
	public static List<V_FriendBean> SelectFriendShipViewmt(Context mContext,final String contactID,final Handler manHandler){
		final List<V_FriendBean> list=new ArrayList<V_FriendBean>();

		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "V_SelectFriendShip");
		mMap.put("contactID", contactID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "V_SelectFriendShip"+contactID, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {		
					if(result.has("result"))
					{
					 JSONArray jsonArray = result.optJSONArray("result");
					 if(jsonArray!=null){
						 V_FriendBean[] uu=null;
						 uu=JSONHelper.parseArray(jsonArray, V_FriendBean.class);
						 for (V_FriendBean friendship : uu) {
							 list.add(friendship);
					        	Log.i("bt","V_SelectFriendShip+val==>"+friendship.get_Name()+">"+friendship.get_ContactMainID());
						}
						 /*Intent intent = new Intent(); 
			             Bundle bundle = new Bundle();
			             bundle.putSerializable("parameter",(Serializable)list);
			             bundle.putSerializable("contactID",contactID);
			             intent.putExtras(bundle);
			             intent.setAction("V_SelectFriendShip");  
			             mContext.sendBroadcast(intent);*/
						 Message mes=new Message();
						 mes.what=constantUtil.Num_SelectFriendByContactId;
						 mes.obj=list;
						 manHandler.sendMessage(mes);
					 }
					} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","V_SelectFriendShip+err==>"+error.getMessage());

			}
		});
		return list;
	}
	
	//mt
	/**
	 * 查询联系人朋友视图
	 * @param mContext
	 * @param contactID
	 */
	public static void SelectFriendShipView(Context mContext,final String contactID){
		final List<V_FriendBean> list=new ArrayList<V_FriendBean>();

		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "V_SelectFriendShip");
		mMap.put("contactID", contactID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forFirendShip, "V_SelectFriendShip"+contactID, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {		
					if(result.has("result"))
					{
					 JSONArray jsonArray = result.optJSONArray("result");
					 if(jsonArray!=null){
						 V_FriendBean[] uu=null;
						 uu=JSONHelper.parseArray(jsonArray, V_FriendBean.class);
						 for (V_FriendBean friendship : uu) {
							 list.add(friendship);
					        	Log.i("bt","V_SelectFriendShip+val==>"+friendship.get_Name()+">"+friendship.get_ContactMainID());
						}
						 Intent intent = new Intent(); 
			             Bundle bundle = new Bundle();
			             bundle.putSerializable("parameter",(Serializable)list);
			             bundle.putSerializable("contactID",contactID);
			             intent.putExtras(bundle);
			             intent.setAction("V_SelectFriendShip");  
			             mContext.sendBroadcast(intent);
					 }
					} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","V_SelectFriendShip+err==>"+error.getMessage());

			}
		});
		
	}
}
